<template>
    <div class="app-container">
        <header-title/>
        <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="110px"
        >
            <el-form-item label="关键词" prop="keyword">
                <el-input
                    v-model="queryParams.keyword"
                    placeholder="请输入订单号/手机号/订单标题"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <!-- <el-form-item label="商户" prop="menDianId">
                <el-select v-model="queryParams.menDianId" filterable clearable>
                    <el-option
                        v-for="dict in userList"
                        :key="dict.menDianId"
                        :label="dict.name "
                        :value="dict.menDianId"
                    >
                    </el-option>
                </el-select>
            </el-form-item> -->
            <el-form-item label="订单状态" prop="orderStatus">
                <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
                    <el-option
                        v-for="dict in dict.type.order_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    >
                    </el-option>
                    
                </el-select>
            </el-form-item>
            
           
            
            <el-form-item label="充值类型" prop="rechargeType">
                <el-select v-model="queryParams.rechargeType" placeholder="请选择充值类型" clearable>
                    <el-option
                        v-for="dict in dict.type.recharge_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            
            
            <el-form-item label="订单发起时间" prop="jiTime">
                <el-date-picker
                    v-model="dateRange"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    @keyup.enter.native="handleQuery"
                    placeholder="选择订单发起时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="订单结束时间" prop="jiTime">
                <el-date-picker
                    v-model="dateRangeTwo"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    @keyup.enter.native="handleQuery"
                    placeholder="选择订单结束时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item class="mar-r-20">
                <el-button type="primary" icon="el-icon-search" @click="handleQuery"
                >搜索</el-button
                >
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                
            </el-form-item>
        </el-form>
        <el-table
            v-loading="loading"
            :data="tableList"
        >   
            <el-table-column label="订单ID" >
                <template slot-scope="scope">
                    {{ scope.row.orderId ||'-' }}
                </template>
            </el-table-column>
           
            <el-table-column label="订单类型" min-width="100">
                <template slot-scope="scope">
                    <span v-if="scope.row.orderType == 2">
                        {{selectDictLabel(dict.type.ai_type,scope.row.useAiType ) ||'-'}}
                    </span>
                    <span v-else>
                        {{selectDictLabel(dict.type.order_type,scope.row.orderType ) ||'-'}}
                    </span>
                    <div v-if="scope.row.orderType == 1">充值类型：{{selectDictLabel(dict.type.recharge_type,scope.row.rechargeType ) ||'-'}}</div>
                </template>
            </el-table-column>
            <!-- <el-table-column label="用户信息" min-width="130">
                <template slot-scope="scope">
                    <el-form size="mini" label-suffix="：" class="form-detail-box">
                        <el-form-item label="">
                        <div class="fw-b">{{ scope.row.orderUserName || "-" }}</div>
                            
                        </el-form-item>
                        <el-form-item label="联系方式" class="label-fw-initial">
                            <span>{{ scope.row.phone || "-" }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column> -->
            <el-table-column label="门店信息" min-width="180">
                <template slot-scope="scope">
                    <el-form size="mini" label-suffix="：" class="form-detail-box" v-if="scope.row.menDian">
                        <el-form-item label="">
                            <div class="fw-b">{{ scope.row.menDian.name || "-" }}</div>
                        </el-form-item>
                        <el-form-item label="联系方式" class="label-fw-initial">
                            <span>{{ scope.row.menDian.leaderName || "-" }}/{{ scope.row.menDian.leaderPhone || "-" }}</span>
                        </el-form-item>
                        
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column label="充值金额" >
                <template slot-scope="scope">
                    {{ scope.row.changePoint ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="订单状态" >
                <template slot-scope="scope">
                    <el-tag
                        v-if="scope.row.orderStatus"
                        :type="
                        [3].includes(scope.row.orderStatus)
                            ? 'success'
                            : [0].includes(scope.row.orderStatus)
                            ? 'info'
                            : [2].includes(scope.row.orderStatus)
                            ? 'danger'
                            : [1].includes(scope.row.orderStatus)
                            ? 'warning':''
                        "
                        >
                        {{
                        selectDictLabel(
                            dict.type.order_status,
                            scope.row.orderStatus
                        ) || "未知状态"
                        }}
                        </el-tag
                    >
                    
                </template>
            </el-table-column>
            <el-table-column label="订单日期" width="140">
                <template slot-scope="scope">
                    {{ scope.row.createTime ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="100">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-search"
                        @click="handleUpdateDetail(scope.row)"
                    >详情</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        >
        </pagination>
        <!-- 订单详情 -->
        <el-drawer
            title="订单详情"
            :visible.sync="showDetail"
            direction="rtl"
            :before-close="handleClose"
            size="900px"
        >
            <orderDetail :id="detailId" v-if="showDetail"></orderDetail>
        </el-drawer>
       
    </div>
</template>
<script>
    import {listOrder} from '@/api/orderManage/orderList'
    import orderDetail from '../components/orderDetail.vue'
    import { mapGetters } from 'vuex'

    export default{
        name:'OrderList',
        components:{orderDetail},
        dicts:['order_status','order_type','point_change_type','recharge_status','recharge_type','ai_type'],
        data() {
            return {
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    orderStatus:'',
                    createTimeEnd: '',
                    createTimeStart:'',
                    endTimeEnd:'',
                    endTimeStart:'',
                    keyword:'',
                    orderType: 1,
                    pointChangeType: '',
                    rechargeStatus: '',
                    rechargeType: '',
                    useAiType:'',
                    userId: '',
                    taskBigStatus:''
                   
                },
                showSearch: true,
                total: 0,
                dateRange: [],
                dateRangeTwo:[],
                loading:true,
                // 表格数据
                tableList: [],
                userList:[],
                showDetail: false,
                detailId:'',
                open : false,
                form: {},
                rules: {
                    menDianId:[
                        { required: true, message: "请先选择门店", trigger: "change" }
                    ],
                    amount:[
                        { required: true, message: "充值金额不能为空", trigger: "change" }
                    ],
                    rechargePoint:[
                        { required: true, message: "充值算力不能为空", trigger: "change" }
                    ],
                },
                isShowMendian: false,
            }
        },
        computed: {
          ...mapGetters(['userInfo'])
        },
        created() {
            this.queryParams.menDianId = this.userInfo.menDianId
            this.getList()
        },
        methods: {
            onHandleAdd() {
                this.isShowMendian = true
                this.form = {
                    menDianId: '',
                    amount:'',
                    rechargePoint:'',
                }
                this.open = true
                    
            },
            
            handleClose() {
                this.showDetail = false
            },
            
            handleUpdateDetail(row) {
                this.detailId = row.orderNo
                this.showDetail = true
            },
             /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.dateRangeTwo = []
                this.resetForm("queryForm");
                this.handleQuery();
            },
            getList() {
                this.loading = true;
                if (this.dateRange && this.dateRange.length) {
                    this.queryParams.createTimeStart = this.dateRange[0];
                    this.queryParams.createTimeEnd = this.dateRange[1];
                } else {
                    this.queryParams.createTimeStart = undefined;
                    this.queryParams.createTimeEnd = undefined;
                }
                if (this.dateRangeTwo && this.dateRangeTwo.length) {
                    this.queryParams.endTimeStart = this.dateRangeTwo[0];
                    this.queryParams.endTimeEnd = this.dateRangeTwo[1];
                } else {
                    this.queryParams.endTimeStart = undefined;
                    this.queryParams.endTimeEnd = undefined;
                }
                
                listOrder(this.queryParams).then((response) => {
                    this.tableList = response.data.records;
                    this.total = response.data.total;
                    this.loading = false;
                });
            }
        }
    }
</script>